package Model;

import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;

public class AdvertisementList {
	
	public  ArrayList<Advertisement> AdvList = new ArrayList<Advertisement>();
	
	private DbConnection db = null;
	private Statement st = null;
	private ResultSet rs = null; 
	
	public AdvertisementList(){
		   db = new DbConnection();	   
		   st = db.getStatement();
		   
		   try{
				this.rs = this.st.executeQuery("Select id_ad,date_pub from advertisement order by date_pub asc;");
				
				Advertisement adv = new Advertisement();
				
				while(this.rs.next()){
					adv.setAdveriement(this.rs.getInt("id_ad"));
					this.AdvList.add(adv);
				}

			}catch (Exception e) {
				System.out.print(e.toString());
			}   
		
	}
	
	public AdvertisementList(int id_user){
		   db = new DbConnection();	   
		   st = db.getStatement();
		   
		   try{
				this.rs = this.st.executeQuery("Select id_ad,date_pub from advertisement where id_ower="+id_user+" order by date_pub asc;");
				
				Advertisement adv = new Advertisement();
				
				while(this.rs.next()){
					adv.setAdveriement(this.rs.getInt("id_ad"));
					this.AdvList.add(adv);
				}
				
			}catch (Exception e) {
				System.out.print(e.toString());
			}   
		
	}
	
	public AdvertisementList(int offset, int count){
		   db = new DbConnection();	   
		   st = db.getStatement();
		   
		   try{
				this.rs = this.st.executeQuery("Select id_ad,date_pub from advertisement order by date_pub asc LIMIT "+count+" offset "+offset+";");
				
				Advertisement adv = new Advertisement();
				
				while(this.rs.next()){
					adv.setAdveriement(this.rs.getInt("id_ad"));
					this.AdvList.add(adv);
				}
				
			}catch (Exception e) {
				System.out.print(e.toString());
			}   
		
	}
}
